import {
    request2
} from "../../utils/request2";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        idNum: null,
        passwd: null,
        hasUserInfo: false,
        userInfo: {},
        _webviewId_: 0,
        code: "",
        msg: ""
    },

    idNumFocus: function (e) {
        this.setData({
            idNum: e.detail.value,
        })
    },

    passwdFocus: function (e) {
        this.setData({
            passwd: e.detail.value,
        })
    },

    //登录按钮
    loginBtn: function () {
        
        if(!this.data.idNum||!this.data.passwd){
            wx - wx.showModal({
                content: '用户名密码不能为空',
                editable: false,
                showCancel: true,
                title: '提示',
            })
            return;
        }
        
        request2({
                url: '/wx/home/login',
                data: {
                    username: this.data.idNum,
                    password: this.data.passwd
                }
            })
            .then(result => {  
               
                this.loginSuccess(result)
               
            })
            .catch((err) => {
                console.log(err);
                wx - wx.showModal({
                    content: '登录失败！',
                    editable: false,
                    showCancel: true,
                    title: '提示',
                })
            })
    },
    //登录前先调用的函数
    loginSuccess: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          
            this.getInfoSuccess(res)
           

            wx.reLaunch({
                url: '../main/main?roleId='+res.data.data.roleId
            })
        } else {
            const msg = res.data.msg
            this.setData({
                msg: msg
            })
            console.log(res.data.msg);
            wx.showToast({
                title: msg,
                icon: 'none',
                duration: 2000
            })
        }
    },
    loginFail: function (res) {
        console.log(res)
        wx.showToast({
            title: "登录失败",
            icon: 'none',
            duration: 2000
        })
    },
    //得到登录成功的信息
    getInfoSuccess: function (res) {
        // console.log(res)
        var isLogin = wx.getStorageSync('isLogin') || "";
        // console.log(isLogin);
        //未登录过
        if (!isLogin ) {
            if (res.data.code== 200) {
                var result = res.data.data
                var password = this.data.passwd
           

                var persInfo = {
                  username: result.username,
                  roleId: result.roleId,
                  mobilePhone: result.mobilePhone,
                  name: result.name,
                  employeeNo: result.employeeNo,
                  sid: result.sid
                }
                wx.setStorageSync('persInfo', persInfo)

                wx.setStorageSync('idCardFace', {
                    "id": this.data.idNum,
                    "password": password
                });

               
                wx.setStorageSync('isLogin', true);
                const app = getApp();
                wx.setStorageSync('sid', result.sid);
            }
        }
        //已登录过
        else {
            if (res.data.code == 200) {
                var result = res.data.data
                var password = wx.getStorageSync('idCardFace').password;
                var id = wx.getStorageSync('idCardFace').id;


                var persInfo = {
                  username: result.username,
                  roleId: result.roleId,
                  mobilePhone: result.mobilePhone,
                  name: result.name,
                  employeeNo: result.employeeNo,
                  sid: result.sid
                }
                wx.setStorageSync('persInfo', persInfo)
               
                wx.setStorageSync('idCardFace', {
                    "id": id,
                    "password": password
                });  
                wx.setStorageSync('isLogin', true);
                
            }
        }


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.isLogin();

    },
    //判断是否登录
    isLogin() {        
        var isLogin = wx.getStorageSync('isLogin');
        if (isLogin == true) {
            
            request2({
                    url: "/wx/home/login",
                    data: {
                        username: wx.getStorageSync('idCardFace').id,
                        password: wx.getStorageSync('idCardFace').password,
                    }
                })
                .then(result => {
                    console.log(result.data.sid);               
                    this.loginSuccess(result)                    
                })

        }
    }  
    
});